package com.example.test.operate.service.impl;

import com.example.test.operate.POJO.PO.OperateFunctionPO;
import com.example.test.operate.POJO.VO.OperateAllotFunctionGetVO;
import com.example.test.operate.POJO.VO.OperateFunctionVO;
import com.example.test.operate.dao.IOperateFunctionDao;
import com.example.test.operate.service.IOperateFunctionDaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <desc>
 *
 * </desc>
 *
 * @createDate 2021-07-22
 */
@Service
public class OperateFunctionDaoServiceImpl implements IOperateFunctionDaoService {

    @Autowired
    IOperateFunctionDao iOperateFunctionDao;
    /**
     * <desc>
     * 通过id获取资源
     * </desc>
     *
     * @param operateFunctionPO 资源实体
     * @return 资源实体
     * @author
     * @createDate
     */
    @Override
    public OperateFunctionPO getOne(OperateFunctionPO operateFunctionPO) {
        return null;
    }

    /**
     * <desc>
     * 获取所有资源
     * </desc>
     *
     * @return 资源实体集合
     * @author
     * @createDate
     */
    @Override
    public List<OperateFunctionPO> getList() {
        return null;
    }


    /**
     * <desc>
     * 获取分配菜单所有节点
     * </desc>
     *
     * @param roleId
     * @return
     * @author
     * @createDate
     */
    @Override
    public OperateAllotFunctionGetVO getAllotFunction(String roleId) {
        //运营菜单
        List<OperateFunctionVO> roots = iOperateFunctionDao.getChildsByParent("0",2);
        List<String> roleFunctions = iOperateFunctionDao.getRoleFunctions(roleId);
        roots = recursionFunction(roots,roleFunctions);
        OperateAllotFunctionGetVO<OperateFunctionVO> operateAllotFunctionGetVO = new OperateAllotFunctionGetVO<>();
        operateAllotFunctionGetVO.setSystemName("运营菜单");
        operateAllotFunctionGetVO.setFunctionList(roots);
        return operateAllotFunctionGetVO;
    }
    /**
     * <desc>
     *      递归获取所有节点 并查询角色是否已有该节点
     * </desc>
     *
     * @return
     * @author
     * @createDate
     */
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<OperateFunctionVO> recursionFunction(List<OperateFunctionVO> roots,List<String> roleFunctions){
        roots.forEach(child->{
            List<OperateFunctionVO> list = iOperateFunctionDao.getChildsByParent(child.getFunctionId(),2);
            //遍历roleFunctions查找第一个与child functionId相同的值，将child的haveAuth设为true
            if(roleFunctions.stream().filter(rf ->
                    rf.equals(child.getFunctionId())
            ).findFirst().isPresent()){
                child.setHaveAuth("true");
            }
            if(list != null && !list.isEmpty()){
                //遍历菜单节点与角色菜单判断是否已有
                list.forEach(function->{
                    if(roleFunctions.stream().filter(rf ->
                            rf.equals(function.getFunctionId())
                    ).findFirst().isPresent()){
                        function.setHaveAuth("true");
                    }
                });
                child.setHaveChild("true");
                child.setChilds(this.recursionFunction(list,roleFunctions));
            }
        });
        return roots;
    }
}
